<?php
namespace app\admin\controller\project;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Financial extends Base
{

    public function index()
    {
        $post = $this->request->param();
        $this->assign('project_id', $post['project_id']);
        return view();
    }

    public function finadd()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = new \app\admin\validate\Financial();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            if ($post['project_financial'] == 8) {
                $this->error('毛利记录自动添加，修改');
            }

            Db::startTrans();
            try {
                $post['c_time'] = time();
                if (isset($post['id']) && !empty($post['id'])) {
                $post['remind_time']=strtotime($post['remind_time']);
                    DB::name('project_financial')->where('id', $post['id'])->update($post);
                } else {
                    $post['uid'] = session('uid');
                    $post['remind_time']=strtotime($post['remind_time']);
                    DB::name('project_financial')->insert($post);
                    if (DB::name('project_financial')->where('project_id',$post['project_id'])->where('financial_type', 0)->find()) {
                   //什么也不做
                    } else {
                        $post['c_time'] = 0;
                        $post['uid'] = session('uid');
                        $post['info'] = '毛利=收入-支出+应收-应付';
                        $post['project_financial'] = 8;
                        $post['financial_type'] = 0;//标记为毛利
                        $post['price'] = 0;
                        DB::name('project_financial')->insert($post);
                    }
                }
                //计算毛利
                $type1 = DB::name('project_financial')->where('financial_type','>',0)->where('project_id',$post['project_id'])->where('financial_type', 1)->sum('price');
                $type2 = DB::name('project_financial')->where('financial_type','>',0)->where('project_id',$post['project_id'])->where('financial_type', 2)->sum('price');
                $type3 = DB::name('project_financial')->where('financial_type','>',0)->where('project_id',$post['project_id'])->where('financial_type', 3)->sum('price');
                $type4 = DB::name('project_financial')->where('financial_type','>',0)->where('project_id',$post['project_id'])->where('financial_type', 4)->sum('price');
                DB::name('project_financial')->where('project_id',$post['project_id'])->where('financial_type', 0)->update(['price' => $type1 - $type2+$type3-$type4]);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        } else {
            if (isset($post['id'])) {
                $info = DB::name('project_financial')->where('id', $post['id'])->find();
                if ($info['project_financial'] == 8) {
                    $this->error('毛利记录不能修改');
                }
                $info['remind_time'] == 0 ? $info['remind_time'] = '' : $info['remind_time'] = date('Y-m-d H:i:s', $info['remind_time']);
                $this->assign('info', $info);
                $post['project_id']=$info['project_id'];
            }
            $project_financial = config('param.project_financial');
            $financial_type = config('param.financial_type');
            $this->assign('project_financial', $project_financial);
            $this->assign('financial_type', $financial_type);
            $this->assign('project_id', $post['project_id']);
            return view();
        }
    }

    public function delete()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $info = DB::name('project_financial')->where('id', $post['id'])->find();
            if ($info['financial_type'] == 0) {
                $this->error('毛利记录不能删除');
            }
            if (DB::name('project_financial')->where('id', $post['id'])->delete()) {
                //计算毛利
                $type1 = DB::name('project_financial')->where('financial_type','>',0)->where('project_id',$info['project_id'])->where('financial_type', 1)->sum('price');
                $type2 = DB::name('project_financial')->where('financial_type','>',0)->where('project_id',$info['project_id'])->where('financial_type', 2)->sum('price');
                $type3 = DB::name('project_financial')->where('financial_type','>',0)->where('project_id',$info['project_id'])->where('financial_type', 3)->sum('price');
                $type4 = DB::name('project_financial')->where('financial_type','>',0)->where('project_id',$info['project_id'])->where('financial_type', 4)->sum('price');
                DB::name('project_financial')->where('project_id',$info['project_id'])->where('financial_type', 0)->update(['price' => $type1 - $type2+$type3-$type4]);
                $this->success('操作成功!刷新列表毛利会更新');
            }
            $this->error('操作失败');
        }
    }


    public function finlists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['project_financial.project_id', '=', $post['project_id']];
        $data['count'] = Db::view('project_financial', 'id,project_id,project_financial,info,financial_type,c_time,uid')
            ->view('admin', 'nickname', 'project_financial.uid=admin.id', 'LEFT')
            ->where($map)
            ->count();
        $data['data'] = Db::view('project_financial', 'id,project_id,project_financial,info,financial_type,c_time,uid,price,remind_time')
            ->view('admin', 'nickname', 'project_financial.uid=admin.id', 'LEFT')
            ->where($map)
            // ->fetchSql(true)
            ->order('c_time desc')->page(input("param.page"), input('param.limit'))->select();
        $project_financial = config('param.project_financial');
        $financial_type = config('param.financial_type');
         $color = config('param.project_color');
        foreach ($data['data'] as &$info) {
         $info['price'] = '<div style="color:' . $color[$info['financial_type']] . '">' . $info['price'] . '</div>';
            $info['financial_type_status']= $info['financial_type']==2?2:1;
            $info['financial_type'] = $info['financial_type']==0?'毛利':$financial_type[$info['financial_type']];
            $info['project_financial'] = $info['project_financial']==0?'--':$project_financial[$info['project_financial']];
            $info['c_time'] == 0 ? $info['c_time'] = '--' : $info['c_time'] = date('Y-m-d H:i:s', $info['c_time']);
            $info['info'] = strip_tags($info['info']);
            $info['remind_time'] == 0 ? $info['remind_time'] = '--' : $info['remind_time'] = date('Y-m-d H:i:s', $info['remind_time']);
        }

        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }
}